/*
 * Copyright (C) 2003 Christopher Stawarz <cstawarz@cfa.harvard.edu>
 *
 * This file is part of SLglade.
 *
 * SLglade is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * SLglade is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with SLglade; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * Parts of this file are taken from SLIRP.  The following copyright
 * applies to those parts:
 *
 * SLIRP: The (Sl)ang (I)nte(r)face (P)ackage is a code generator,
 * like S.W.I.G. for S-Lang.
 *
 * Copyright (C) 2003 Massachusetts Institute of Technology 
 * Copyright (C) 2002 Michael S. Noble <mnoble@space.mit.edu>
 *
 * This software was partially developed by the MIT Center for Space
 * Research under contract SV1-61010 from the Smithsonian Institution.
 * 
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear in
 * the supporting documentation, and that the name of the Massachusetts
 * Institute of Technology not be used in advertising or publicity
 * pertaining to distribution of the software without specific, written
 * prior permission.  The Massachusetts Institute of Technology makes
 * no representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 * 
 * THE MASSACHUSETTS INSTITUTE OF TECHNOLOGY DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL THE MASSACHUSETTS
 * INSTITUTE OF TECHNOLOGY BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


#include <stdlib.h>
#include <slang.h>

#include <glade/glade.h>
#include <glade/glade-init.h>
#include <glade/glade-xml.h>


#define SLGLADE_MAJOR_VERSION	0
#define SLGLADE_MINOR_VERSION	0
#define SLGLADE_PATCH_LEVEL	1
static char *slglade_version_string = "0.0.1";

#define SLGLADE_VERSION_NUMBER	\
   (SLGLADE_MAJOR_VERSION*10000+SLGLADE_MINOR_VERSION*100+SLGLADE_PATCH_LEVEL)

static SLang_IConstant_Type glade_IConsts [] = 
{
  MAKE_ICONSTANT("_glade_module_version", SLGLADE_VERSION_NUMBER),
  SLANG_END_ICONST_TABLE
};

static SLang_Intrin_Var_Type glade_IVars[] =
{
  MAKE_VARIABLE("_glade_module_version_string", &slglade_version_string,
    SLANG_STRING_TYPE, 1),
  SLANG_END_INTRIN_VAR_TABLE
};

#define   MODULE_HAS_OPAQUE_TYPE	23

#include "slirptypes.h"

unsigned char   map_scalars_to_refs = 0;

static int pop_array_or_ref(int type, void *pv, Slirp_Ref *sref)
{
   unsigned int objtype = SLang_peek_at_stack();

   switch(objtype) {

	case SLANG_ARRAY_TYPE:
	   if (SLang_pop_array_of_type(&sref->array,type) == -1)
		return -1;

           sref->ref = NULL;
	   sref->data = sref->array->data;
	   break;

	case SLANG_REF_TYPE:
	   if (SLang_pop_ref(&sref->ref) == -1)
		return -1;
	   sref->array = NULL;
	   sref->data = pv;
	   break;

	case SLANG_CHAR_TYPE:		/* treat scalar instances of basic */
	case SLANG_UCHAR_TYPE:		/* types as 1-elementn arrays      */
	case SLANG_SHORT_TYPE:
	case SLANG_USHORT_TYPE:
	case SLANG_INT_TYPE:
	case SLANG_UINT_TYPE:
	case SLANG_LONG_TYPE:
	case SLANG_ULONG_TYPE:
	case SLANG_FLOAT_TYPE:
	case SLANG_DOUBLE_TYPE:

	{
	   /* Accomodate FORTRAN pass by reference semantics */

           sref->ref = NULL;
	   if (map_scalars_to_refs && 
		 	SLang_pop_array_of_type(&sref->array,type) == 0) {

           	memcpy(pv,sref->array->data,(size_t)sref->array->sizeof_type);
		sref->data = pv;
		break;
	   }
	}
	   /* intentional fallthrough */

	default:

	   SLang_verror(SL_INTRINSIC_ERROR, "input argument is not an array");
	   return -1;
   }

   return 0;
}

#include "glade_init.h"
#include "glade_glue.c"

#include "intrin_funcs.c"

static SLang_Intrin_Fun_Type glade_Intrin_Funcs[] =
{
#include "glade_ftable.c"
  MAKE_INTRINSIC_0("glade_xml_new_from_buffer",slglade_xml_new_from_buffer,V),
  MAKE_INTRINSIC_0("glade_xml_signal_connect",slglade_xml_signal_connect,V),
  MAKE_INTRINSIC_0("glade_xml_signal_autoconnect",slglade_xml_signal_autoconnect,V),
  MAKE_INTRINSIC_0("_glade_xml_get_widget_prefix",slglade_xml_get_widget_prefix,V),
  SLANG_END_INTRIN_FUN_TABLE
};

#include "slirptypes.c"

SLANG_MODULE(glade);
int init_glade_module_ns(char *ns_name)
{
   SLang_NameSpace_Type *ns = NULL;

   /* Before doing anything, check that SLgtk has been loaded */
   if (-1 != SLang_is_defined("_slgtk_version_string")) {
	SLang_verror(SL_INTRINSIC_ERROR,"gtk module has not been loaded");
        return -1;
   }

   if (ns_name != NULL) {
	ns = SLns_create_namespace (ns_name);
	if (ns == NULL)
	   return -1;
   }

   if (allocate_glade_opaque_types() == -1)
        return -1;

   if (-1 == SLns_add_iconstant_table (ns, glade_IConsts, NULL))
	return -1;

   if (-1 == SLns_add_intrin_var_table (ns, glade_IVars, NULL))
	return -1;

   if (-1 == SLns_add_intrin_fun_table (ns, glade_Intrin_Funcs,"__glade__"))
	return -1;

#include "init_load_string.c"

   return 0;
}

